﻿using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace SharpAnalyzer.Analyzers
{
    internal sealed class TypeClassCouplingAnalyzer : ClassCouplingAnalyzerBase
    {
        public TypeClassCouplingAnalyzer(SemanticModel semanticModel) : base(semanticModel)
        {
        }

        public IEnumerable<string> Calculate(TypeDeclarationSyntax typeNode)
        {
            Visit(typeNode);
            return GetCollectedTypesNames();
        }

        public override void VisitFieldDeclaration(FieldDeclarationSyntax node)
        {
            base.VisitFieldDeclaration(node);
            FilterType(node.Declaration.Type);
        }

        public override void VisitClassDeclaration(ClassDeclarationSyntax node)
        {
            base.VisitClassDeclaration(node);
            if (node.BaseList == null)
                return;

            INamedTypeSymbol symbolInfo = SemanticModel.GetDeclaredSymbol(node) as INamedTypeSymbol;
            if (symbolInfo == null || symbolInfo.BaseType == null)
                return;
            FilterTypeSymbol(symbolInfo.BaseType);
            return;
        }
    }
}
